package com.cc.cbdb.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.RelativeLayout;

import com.cc.cbdb.R;
import com.cc.cbdb.common.report.RecordSQLiteOpenHelper;

import java.lang.ref.WeakReference;

public class SplashActivity extends Activity {

    private static final int ENTER_HOME = 100;
    private RelativeLayout splash_layout;

    private MsgHandler handler;

    private static class MsgHandler extends Handler{
        private WeakReference<SplashActivity> reference;

        MsgHandler(SplashActivity activity){
            reference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            SplashActivity activity = reference.get();
            switch (msg.what){
                case ENTER_HOME:
                    activity.enterHome();
                    break ;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        splash_layout = findViewById(R.id.splash_layout);
        init();
    }

    /**
     * 软件初始化
     */
    public void init(){
        handler = new MsgHandler(this);
        initData();
    }

    private void initData() {
        //初始化数据库
        new RecordSQLiteOpenHelper(this).getWritableDatabase();
        handler.sendEmptyMessageDelayed(ENTER_HOME,2500);
    }

    /**
     * 进入主界面
     */
    private void enterHome() {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
        finish();
    }
}
